﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace dmp_ui_demo
{
    public partial class ArticleEdit : Form
    {
        DataTable dt;
        DataTable dt_task;
        BrowserBridge bridge;
        public ArticleEdit()
        {
            InitializeComponent();
            bridge = new BrowserBridge(webBrowser1, false, string.Empty);
            bridge.OnDataSave += bridge_OnDataSave;
            return;



            Article a = Deserialize(@"d:\test.xml", typeof(Article)) as Article;
            //Article a = new Article();
            a.Title = "test";
            a.Edittime = string.Empty;
            a.Paragraphs.Add(new Paragraph { Title = "test", Content = "contentteest" });
            a.Paragraphs.Add(new Paragraph { Title = "test1", Content = "contentteest" });
            a.Paragraphs.Add(new Paragraph { Title = "tes2t", Content = "contentteest" });
            a.Paragraphs.Add(new Paragraph { Title = "test3", Content = "contentteest" });
            Serialiaze(@"d:\test.xml", a); return;



            List<HighlightKey> keys = new List<HighlightKey>();
            keys.Add(new HighlightKey { Key = "test", BackColor = Color.Red });
            keys.Add(new HighlightKey { Key = "test", BackColor = Color.Red });
            string k = ObjToJson(keys);
            Debug.WriteLine(k);
            Serialiaze(@"d:\test.xml", keys);
        }

        void bridge_OnDataSave(string identity, string title, string content)
        {
            Debug.WriteLine(identity);
        }

        private void Window_Load(object sender, HtmlElementEventArgs e)
        {
            Debug.WriteLine("Window_Load");
        }

        public void getData()
        {
            dt = new DataTable();

            DataColumn dc_articletitle = new DataColumn("articletitle", Type.GetType("System.String"));
            DataColumn dc_updatetime = new DataColumn("updatetime", Type.GetType("System.String"));

            dt.Columns.Add(dc_articletitle);
            dt.Columns.Add(dc_updatetime);

            for (int i = 0; i < 10; i++)
            {
                DataRow dr = dt.NewRow();
                dr["articletitle"] = "文章标题" + i;
                dr["updatetime"] = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                dt.Rows.Add(dr);
            }

            dt_task = new DataTable();

            DataColumn dc_tasktitle = new DataColumn("tasktitle", Type.GetType("System.String"));
            DataColumn dc_edituser = new DataColumn("edituser", Type.GetType("System.String"));
            DataColumn dc_checkuser = new DataColumn("checkuser", Type.GetType("System.String"));
            DataColumn dc_taskstatus = new DataColumn("taskstatus", Type.GetType("System.String"));
            DataColumn dc_articlestatus = new DataColumn("articlestatus", Type.GetType("System.String"));

            dt_task.Columns.Add(dc_tasktitle);
            dt_task.Columns.Add(dc_edituser);
            dt_task.Columns.Add(dc_checkuser);
            dt_task.Columns.Add(dc_taskstatus);
            dt_task.Columns.Add(dc_articlestatus);

            for (int i = 0; i < 10; i++)
            {
                DataRow dr = dt_task.NewRow();
                dr["tasktitle"] = "任务" + i;
                dr["edituser"] = "任务人" + i;
                dr["checkuser"] = "审稿人" + i;
                dr["taskstatus"] = "进行中";
                dr["articlestatus"] = "签出";
                dt_task.Rows.Add(dr);
            }
        }

        private void ArticleEdit_Load(object sender, EventArgs e)
        {
            getData();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

            string html = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\CkeditorPlugins\debug.htm"));
            webBrowser1.Url = new Uri(html);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            Debug.WriteLine("webBrowser1_DocumentCompleted");
            var styles = webBrowser1.Document.GetElementsByTagName("style");
            for (int i = 0; i < styles.Count; i++)
            {
                if (styles[i].GetAttribute("media") == "screen")
                {
                    //styles[i].InnerHtml = " body { font-size: 14px; padding: 5px; line-height: 1.6em;}";
                }
            }
            var element = webBrowser1.Document.CreateElement("div");
            element.InnerHtml = "test";
            webBrowser1.Document.Body.AppendChild(element);
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            Debug.WriteLine("webBrowser1_Navigating");
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            Debug.WriteLine("webBrowser1_Navigated");
            webBrowser1.Document.Window.Load += Window_Load;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            bridge.Goto("identity19");
        }

        public string ObjToJson(object obj)
        {
            System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
            string json = ser.Serialize(obj);
            return json;
        }

        public static void Serialiaze(string filename, object obj)
        {
            XmlSerializer xs = new XmlSerializer(obj.GetType());
            Stream stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
            xs.Serialize(stream, obj);
            stream.Close();
            stream.Dispose();
        }

        public static object Deserialize(string filename, Type type)
        {
            if (File.Exists(filename))
            {
                using (StreamReader reader = new StreamReader(filename))
                {
                    XmlTextReader xmlReader = new XmlTextReader(reader);
                    // 处理空格
                    xmlReader.WhitespaceHandling = WhitespaceHandling.All;
                    XmlSerializer xs = new XmlSerializer(type);
                    return xs.Deserialize(xmlReader);
                }
            }
            else
            {
                return null;
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            webBrowser1.Document.Body.Style = "font-size:40px;";
            webBrowser1.ShowPrintPreviewDialog();
        }
    }
}
